﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using VideoClassLibrary.Model;
using VideoClassLibrary.BusinessLogicLayer;
public partial class AdminManagement_UpdateAdmin : AdminUserBase
{
    AdminModule adminmodel = new AdminModule();
    protected void Page_Load(object sender, EventArgs e)
    {
        this.rbtnListType.Enabled = true;
        this.hidAdminType.Value = this.AdminType.ToString();
        switch (this.AdminType)
        {
            case 0:
                this.rbtnListType.Items[0].Enabled = true;
                this.rbtnListType.Items[1].Enabled = true;
                this.rbtnListType.Items[2].Enabled = false;
                this.rbtnListType.Items[3].Enabled = false;
                this.rbtnListType.Items[4].Enabled = false;
                break;
            case 1:
                if (!string.IsNullOrEmpty(Request.QueryString["AdID"].ToString()))
                {
                    if (this.UserID == int.Parse(Request.QueryString["AdID"].ToString()))
                    {
                        this.rbtnListType.Enabled = false;
                    }
                    else
                    {
                        this.rbtnListType.Items[0].Enabled = false;
                        this.rbtnListType.Items[1].Enabled = false;
                        this.rbtnListType.Items[2].Enabled = true;
                        this.rbtnListType.Items[3].Enabled = true;
                        this.rbtnListType.Items[4].Enabled = true;
                    }
                }
                else
                {
                    this.rbtnListType.Items[0].Enabled = false;
                    this.rbtnListType.Items[1].Enabled = false;
                    this.rbtnListType.Items[2].Enabled = true;
                    this.rbtnListType.Items[3].Enabled = true;
                    this.rbtnListType.Items[4].Enabled = true;
                }
                this.ddlSchoolList.Enabled = false;
                break;
            case 2:
            case 3:
            case 4:                
                this.rbtnListType.Items[0].Enabled = false;
                this.rbtnListType.Items[1].Enabled = false;
                this.rbtnListType.Items[2].Enabled = true;
                this.rbtnListType.Items[3].Enabled = true;
                this.rbtnListType.Items[4].Enabled = true;
                this.ddlSchoolList.Enabled = false;
                break;

        }
        if (!IsPostBack)
        {
            //TeacherModule ta = new TeacherModule();
            //List<V_School> vs = new List<V_School>();
            //vs = ta.GetSchool();
            //ddlSchoolList.DataSource = vs;
            //ddlSchoolList.DataBind();
            BindSchoolList();
            int adid = Convert.ToInt32(Request.QueryString["AdID"].ToString());
            V_adminUser admin = new V_adminUser();
            admin = adminmodel.GetAdminModelById(adid);
            txtUname.Text = admin.VA_loginname;
            realname.Text = admin.VA_realname;
            txtpwd.Text = "";
            txttelephone.Text = admin.VA_mobile;
            txtemail.Text = admin.VA_email;
            rbtnListType.SelectedValue = admin.VA_type.ToString();
            ddlSchoolList.SelectedValue = admin.SchoolID.ToString();
            this.chkIsEnable.Checked = admin.Va_IsEnable == 1 ? true : false;

        }

    }
    public void BindSchoolList()
    {
        SchoolModule schoolHelper = new SchoolModule();
        DataSet dsSchool = schoolHelper.GetSchoolListByWhere("");
        if (dsSchool != null && dsSchool.Tables.Count > 0 && dsSchool.Tables[0].Rows.Count > 0)
        {
            DataRow dr = null;
            ddlSchoolList.Items.Clear();
            ddlSchoolList.Items.Add(new ListItem("请选择", "0"));
            for (int i = 0; i < dsSchool.Tables[0].Rows.Count; i++)
            {
                dr = dsSchool.Tables[0].Rows[i];
                ddlSchoolList.Items.Add(new ListItem(dr["VS_name"].ToString(), dr["ID"].ToString()));
            }

        }
    }
    protected void btnRegister_Click(object sender, EventArgs e)
    {
        V_adminUser admin = new V_adminUser();
        admin.ID = Convert.ToInt32(Request.QueryString["AdID"].ToString());
        admin.VA_loginname = txtUname.Text.Trim();
        admin.VA_realname = realname.Text.Trim();
        admin.VA_password = txtpwd.Text.Trim();
        admin.VA_mobile = txttelephone.Text.Trim();
        admin.VA_email = txtemail.Text.Trim();
        admin.VA_type = Convert.ToInt32(rbtnListType.SelectedValue.ToString());
        admin.SchoolID = rbtnListType.SelectedValue == "0" ? 0 : int.Parse(ddlSchoolList.SelectedValue);
        admin.Update_time = DateTime.Now;
        admin.Va_IsEnable = this.chkIsEnable.Checked ? 1 : 0;
        try
        {
            int result = adminmodel.UpdateAdminUser(admin);
            if (result == 1)
            {
                PageHelper.AlterMsgAndRedirect("修改管理员信息成功！", "AdminList.aspx");

            }
            else { lblMess.Text = "修改管理员信息失败"; }
        }
        catch (Exception ex)
        {
            lblMess.Text = "修改管理员信息失败，请稍后再试";
        }
    }
}
